package com.zte.shopping.service.impl;

import com.zte.shopping.constant.DictConstant;
import com.zte.shopping.entity.Item;
import com.zte.shopping.entity.Order;
import com.zte.shopping.entity.Sequence;
import com.zte.shopping.entity.User;
import com.zte.shopping.exception.UserNotLoginException;
import com.zte.shopping.mapper.item.ItemMapper;
import com.zte.shopping.mapper.order.OrderMapper;
import com.zte.shopping.mapper.sequence.SequenceMapper;
import com.zte.shopping.service.OrderService;
import com.zte.shopping.vo.CartVo;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: ssm
 * @description: 订单service实现类
 * @author: zuti
 * @create: 2020-06-30 18:03
 **/
@Service
public class OrderServiceImpl implements OrderService {

    private  static  final Logger logger= Logger.getLogger(DeptServiceImpl.class);
    @Autowired
    SequenceMapper sequenceMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ItemMapper itemMapper;

    /**
     * @param user
     * @return java.util.List<com.zte.shopping.entity.Order>
     * @Author zuti
     * @Description //TODO 查询用户订单
     * @Date 18:05 2020/6/30
     * @Param [user]
     */
    @Override
    public List<Order> findByUserId(User user) throws UserNotLoginException {


        if(user.getUserId().equals("") || user.getUserId()==null){
            logger.debug("用户Id为空");
            throw  new UserNotLoginException("用户未登录");
        }else{
            logger.debug("OrderServiceImpl===>findByUserId params:user.userId="+user.getUserId());
        }
        Integer userId= user.getUserId();

        // 根据userId查询对应的订单信息
        List<Order> orderList = orderMapper.selectByUserId(userId);


        return orderList;
    }

    /**
     * @param user 订单对应的用户
     * @param cartVo 订单对应的购物车
     * @return java.lang.String
     * @Author zuti
     * @Description //TODO 创建订单
     * @Date 21:20 2020/6/30
     * @Param [user, cart]
     * @Parem user
     * @Parem cart
     */
    @Override
    public String createOrder(User user, CartVo cartVo) {
        Order order = new Order();
        order.setPrice(cartVo.getPrice());
        order.setUser(user);

        // 生成订单号
        // 订单号生成规则:  DD + 年月日  + 序列化
        // 根据订单号前缀去Sequence表中查询对应信息
        // 其中  DictConstant.ORDER_NO_PREFIX 表示   订单编号前缀
        Sequence selectSequence = sequenceMapper.selectByName(DictConstant.ORDER_NO_PREFIX);

        // 若不存在,则新建
        if (selectSequence == null)
        {
            Sequence sequence = new Sequence();

            sequence.setName(DictConstant.ORDER_NO_PREFIX);
            sequence.setValue(DictConstant.ORDER_NO_SEQUENCE_MIN);

            sequenceMapper.insertSequence(sequence);

            order.setNo(DictConstant.ORDER_NO_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequence.getValue());
        }else
        {
            // 若存在,则修改
            String value = selectSequence.getValue();

            if (DictConstant.ORDER_NO_SEQUENCE_MAX.equals(value))
            {
                // 如果原来的序列号达到了最大值
                // 则初始化为最小值,从头开始
                value = DictConstant.ORDER_NO_SEQUENCE_MIN;
            }else
            {
                // 将原来的序列号 + 1
                value = String.format("%06d", Integer.parseInt(value) + 1);
            }

            selectSequence.setValue(value);

            sequenceMapper.updateSquenceValue(DictConstant.ORDER_NO_PREFIX, value);

            order.setNo(DictConstant.ORDER_NO_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + value);
        }


        // 生成订单信息
        orderMapper.insertOrder(order);

        // 对该订单名下的所有的明细信息进行保存
        // 购物车中每一条明细对应一条数据

        List<Item> items = cartVo.getItems();
        for (Item item : items)
        {
            item.setOrder(order);

            itemMapper.insertItem(item);
        }

        return order.getNo();
    }

    /**
     * @param orderId 订单Id
     * @return com.zte.shopping.entity.Order
     * @Author zuti
     * @Description //TODO 查询订单详情
     * @Date 21:31 2020/6/30
     **/
    @Override
    public Order findByOrderId(String orderId) {
        return orderMapper.selectByOrderId(Integer.parseInt(orderId));
    }
}
